public class test1 {
    //力扣  2.两数相加
    //自己写的，麻烦
    public ListNode addTwoNumbers1(ListNode l1, ListNode l2) {
        ListNode cur1 = l1;
        ListNode cur2 = l2;
        ListNode head3 = null;
        ListNode cur3 = head3;
        //记录是否进位
        boolean flag = false;
        while(cur1!=null && cur2!=null){
            int num = cur1.val+cur2.val;
            ListNode node = null;
            if(flag){
                node = new ListNode((num+1)%10);
                //判断是否因进位+1而进位的，如果是则不因该修改它的值
                if(num+1<=9){
                    flag=false;
                }
            }else{
                node = new ListNode(num%10);
            }

            if(head3==null){
                head3 =cur3= node;
            }else{
                cur3.next = node;
                cur3 = cur3.next;
            }

            if(num>9){
                flag=true;
            }

            cur1 = cur1.next;
            cur2 = cur2.next;

        }

        //处理剩余的结点
        while(cur1!=null){
            ListNode node = null;
            if(flag){
                node = new ListNode((cur1.val+1)%10);
                if(cur1.val+1<=9){
                    flag=false;
                }
            }else{
                node = new ListNode(cur1.val%10);
            }
            cur3.next = node;
            cur3 = cur3.next;

            cur1 = cur1.next;
        }
        while(cur2!=null){
            ListNode node = null;

            if(flag){
                node = new ListNode((cur2.val+1)%10);
                if(cur2.val+1<=9){
                    flag=false;
                }
            }else{
                node = new ListNode(cur2.val%10);
            }
            cur3.next = node;
            cur3 = cur3.next;

            cur2 = cur2.next;
        }

        if(flag){
            ListNode node = new ListNode(1);
            cur3.next = node;
            cur3 = cur3.next;
        }
        return head3;
    }
}
